﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Drawing;
using System.Web.SessionState;
using Maticsoft.Common;
using System.Text;

namespace Admin.CheckupForm
{
    public partial class Manage : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputButton btnCancel;
        public string adminname = "管理部门";
        ChinaSoft.BLL.BLL_tb_MidSoft_CheckupForm bllCheckupForm = new ChinaSoft.BLL.BLL_tb_MidSoft_CheckupForm();
        ChinaSoft.Model.Model_tb_MidSoft_CheckupForm modelCheckupForm = new ChinaSoft.Model.Model_tb_MidSoft_CheckupForm();
        
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                //imgBtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                if (Request.Params["ProjectId"] != null && Request.Params["ProjectId"].Trim() != "")
                {
                    int ProjectInfoId = ChinaSoft.Common.Input.SetInt(Request.Params["ProjectId"]);
                    BindProjectNameData(ProjectInfoId);
                    this.txtContent.Text = string.Empty;
                    ChinaSoft.BLL.Common.SerialNumber bllSN = new ChinaSoft.BLL.Common.SerialNumber();
                    lblNumber.Text = bllSN.GetSerialNumber("tb_MidSoft_CheckupForm", "CheckupNumber", DateTime.Now.Year.ToString(),5);
                }
                if (Request.Params["CheckupFormId"] != null && Request.Params["CheckupFormId"].Trim() != "")//修改
                {
                    int CheckupFormId = ChinaSoft.Common.Input.SetInt(Request.Params["CheckupFormId"]);
                    BindCheckupFormInfoData(CheckupFormId);
                }
                BindData();
            }
        }
 
        private void BindCheckupFormInfoData(int checkupformId)
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = new DataSet();
            if (checkupformId != 0)
                ds = bllBind.GetDataSet("ProjectNumber,ProjectName,CheckupType,CheckupNumber,CheckupDate,CheckupContent", "V_CheckupFormList", "CheckupFormId=" + checkupformId);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataRow dr=ds.Tables[0].Rows[0];
                this.lblProjectNumber.Text = dr["ProjectNumber"].ToString();
                this.lblProjectName.Text = dr["ProjectName"].ToString();
                ddlType.SelectedValue = dr["CheckupType"].ToString();
                lblNumber.Text = dr["CheckupNumber"].ToString();
                txtContent.Text = dr["CheckupContent"].ToString();
            }
        }
        /// <summary>
        /// 获取项目名称
        /// </summary>
        /// <param name="ProjectInfoId"></param>
        private void BindProjectNameData(int ProjectInfoId)
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = new DataSet();
            if (ProjectInfoId != 0)
                ds = bllBind.GetDataSet("ProjectNumber,ProjectName", "tb_MidSoft_ProjectInfo", "ProjectInfoId=" + ProjectInfoId);
            if (ds != null && ds.Tables.Count > 0)
            {
                this.lblProjectNumber.Text = ds.Tables[0].Rows[0]["ProjectNumber"].ToString();
                this.lblProjectName.Text = ds.Tables[0].Rows[0]["ProjectName"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtContent.Text.Trim().Length == 0)
            {
                strErr += "签审内容不能为空！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            int ProjectInfoId =0;
            if (Request.Params["ProjectId"] != null && Request.Params["ProjectId"].Trim() != "")
                ProjectInfoId = ChinaSoft.Common.Input.SetInt(Request.Params["ProjectId"]);
            string CheckupType = this.ddlType.SelectedValue;
            string CheckupNumber = this.lblNumber.Text;
            DateTime CheckupDate = DateTime.Now;
            string CheckupContent = ChinaSoft.Common.Input.FilterStr(this.txtContent.Text);
            DateTime CreateTime = DateTime.Now;
            int CreateUserId = 0;
            if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);


            if (Request.Params["CheckupFormId"] != null && Request.Params["CheckupFormId"].Trim() != "")//修改
            {
                int checkupFromId = ChinaSoft.Common.Input.SetInt(Request.Params["CheckupFormId"]);
                #region 修改
                modelCheckupForm = bllCheckupForm.GetModel(checkupFromId);
                modelCheckupForm.CheckupFormId = checkupFromId;
                modelCheckupForm.ProjectInfoId = ProjectInfoId;
                modelCheckupForm.CheckupType = CheckupType;
                modelCheckupForm.CheckupNumber = CheckupNumber;
                modelCheckupForm.CheckupContent = CheckupContent;
                bllCheckupForm.Update(modelCheckupForm);
                #endregion

                #region 修改操作日志
                ChinaSoft.Model.Model_tb_MidSoft_log modelLog = new ChinaSoft.Model.Model_tb_MidSoft_log();
                ChinaSoft.BLL.BLL_tb_MidSoft_log bllLog = new ChinaSoft.BLL.BLL_tb_MidSoft_log();
                modelLog.userAccount = "";
                if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                    modelLog.userAccount = Session["UserAccount"].ToString();
                modelLog.userName = "";
                if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                    modelLog.userName = Session["UserName"].ToString();
                modelLog.description = "签审单信息修改,ID=" + checkupFromId;
                modelLog.LogType = 2;
                modelLog.CreateTime = DateTime.Now;
                bllLog.Add(modelLog);
                #endregion

                Response.Redirect("/CheckupForm/Manage.aspx?CheckupFormId=" + Request.Params["CheckupFormId"]);
            }
            else
            {
                #region 添加
                modelCheckupForm.ProjectInfoId = ProjectInfoId;
                modelCheckupForm.CheckupType = CheckupType;
                modelCheckupForm.CheckupNumber = CheckupNumber;
                modelCheckupForm.CheckupDate = CheckupDate;
                modelCheckupForm.CheckupContent = CheckupContent;
                modelCheckupForm.CreateTime = CreateTime;
                modelCheckupForm.CreateUserId = CreateUserId;
                int checkupFormId = bllCheckupForm.Add(modelCheckupForm);
                #endregion

                #region 添加日志
                ChinaSoft.Model.Model_tb_MidSoft_log modelLog = new ChinaSoft.Model.Model_tb_MidSoft_log();
                ChinaSoft.BLL.BLL_tb_MidSoft_log bllLog = new ChinaSoft.BLL.BLL_tb_MidSoft_log();
                modelLog.userAccount = "";
                if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                    modelLog.userAccount = Session["UserAccount"].ToString();
                modelLog.userName = "";
                if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                    modelLog.userName = Session["UserName"].ToString();
                modelLog.description = "签审单信息添加,ID=" + checkupFormId;
                modelLog.LogType = 1;
                modelLog.CreateTime = DateTime.Now;
                bllLog.Add(modelLog);
                #endregion
                Response.Redirect("/CheckupForm/Manage.aspx?ProjectId="+ProjectInfoId);
            }
            BindData();
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            //bllCheckupForm.DeleteList(idlist);
            BindData();
        }

        #region gridView
        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion
            StringBuilder strWhere = new StringBuilder();

            if (Request.Params["CheckType"] != null && Request.Params["CheckType"].Trim() != "")
            {
                strWhere.AppendFormat(" and CheckupType='{0}'", Request.Params["CheckType"]);
            }
            if (txtCheckupFormNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and CheckupNumber like '%{0}%'", txtCheckupFormNumber.Text.Trim());
            }
            if (ddlSearchType.SelectedIndex>0)
            {
                strWhere.AppendFormat(" and CheckupType='{0}'", (ddlSearchType.SelectedIndex-1));
            }
            try
            {
                ChinaSoft.BLL.Common.Pager.BindGridView(gridView, AspNetPager1, "V_CheckupFormList", "CheckupFormId", "*", strWhere.ToString(), "CreateTime desc", "");
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误：" + ex.Message);
                Response.End();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input type=\"checkbox\" id=\"cbSelectAll\" name=\"cbSelectAll\" onclick=\"javascript:checkJs(this.checked);\" />";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                if (linkbtnDel.Attributes["BindData"].ToString() == ChinaSoft.Common.Input.SetString(Session["currentUserID"]))
                    linkbtnDel.Visible = true;
                else
                    linkbtnDel.Visible = false;
                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}

            }
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            modelCheckupForm=bllCheckupForm.GetModel(ID);
            if (modelCheckupForm != null)
            {
                int createUserId=0;
                if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                    createUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);

                if (modelCheckupForm.CreateUserId == createUserId)
                {
                    bllCheckupForm.Delete(ID);
                    BindData();
                }
                else
                    Maticsoft.Common.MessageBox.Show(this,"这不是您的签审单，您没有权限删除该签审单");
            }
            else
                Maticsoft.Common.MessageBox.Show(this, "该签审单以不存在！");
            //bll.Delete(ID);
            //gridView.OnBind();
        }
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("checkboxname");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        #endregion

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }
    }
}